package com.jyxd.web.service.assessment.basic;

import com.jyxd.web.dao.basic.AbpDao;
import com.jyxd.web.data.basic.Abp;
import com.jyxd.web.data.dto.BaseDataDto;
import com.jyxd.web.data.dto.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import static com.jyxd.web.util.UUIDUtil.getUUID;

@Service
@Transactional
public class AbpService {

    @Autowired
    private AbpDao abpDao;

    public boolean insert(Abp abp) {
        return abpDao.insert(abp);
    }

    public boolean update(Abp abp) {
        return abpDao.update(abp);
    }

    public boolean delete(Abp abp) {
        return abpDao.delete(abp);
    }

    /**
     * 批量添加
     *
     * @param listNew
     * @return
     */
    public boolean insertBatch(List<Abp> listNew) {
        return abpDao.insertBatch(listNew);
    }

    /**
     * 根据rowId查询数据
     *
     * @param rowId
     */
    public List<Abp> queryListByRowId(String rowId) {
        return abpDao.queryListByRowId(rowId);
    }

    /**
     * 查询某个病人的Abp
     *
     * @param patientId
     */
    public List<Abp> queryDataNew(String patientId) {
        return abpDao.queryDataNew(patientId);
    }

    public void deleteByRelevancyId(String relevancyId) {
        Abp abp = abpDao.queryByRelevancyId(relevancyId);
        if (Objects.isNull(abp))
            return;
        abp.setStatus(-1);
        delete(abp);
    }

    /**
     * 通过id查询对象
     *
     * @param id
     * @return
     */
    public Abp queryData(String id) {
        return abpDao.queryData(id);
    }

    /**
     * ABP分页数据
     *
     * @param map
     * @return
     */
    public HttpResult queryListByPatientIdAndPageSize(Map<String, Object> map) {
        Map<String, Object> mapData = new HashMap<String, Object>();
        int start = 0;
        int size = Integer.parseInt(map.get("size").toString());
        int sum = abpDao.queryNumByPatientId(map.get("patientId").toString());
        int maxStart = sum / size + 1;
        if (map.get("start") == null) {
            start = maxStart;
        } else {
            start = Integer.parseInt(map.get("start").toString());
        }
        int pageSize = (start - 1) * size;
        map.put("start", pageSize);
        List<Abp> list = abpDao.queryDataByPageAndPatientId(map);

        if (list == null || list.size() == 0) {
            for (int i = 0; i < size; i++) {
                Abp abp = new Abp();
                list.add(abp);
            }

        } else {
            if (list.size() < 46) {
                int sumSize = size - list.size();
                for (int i = 0; i < sumSize; i++) {
                    Abp abp = new Abp();
                    list.add(abp);
                }
            }

        }
        mapData.put("maxStart", maxStart);
        mapData.put("start", start);
        mapData.put("data", list);
        return new HttpResult(200, mapData, "保存成功");
    }

    public boolean saveOrUpdate(BaseDataDto baseDataDto) {
        if (Objects.isNull(baseDataDto))
            return false;
        Abp abp = abpDao.queryByRelevancyId(baseDataDto.getKeyDistinguishId());
        boolean isNewAbp = false;
        if (Objects.isNull(abp)) {
            abp = new Abp();
            abp.setId(getUUID());
            abp.setStatus(1);
            abp.setCreateTime(new Date());
            abp.setRelevancyId(baseDataDto.getKeyDistinguishId());
            isNewAbp = true;
        }
        abp.setPatientId(baseDataDto.getOtherId());
        abp.setContent(baseDataDto.getContent());
        abp.setDataTime(baseDataDto.getTime());
        abp.setSignature(baseDataDto.getSign());
        if (isNewAbp)
            abpDao.insert(abp);
        else
            abpDao.update(abp);
        return true;
    }
}
